/**
 * 
 */
package us.gaaoc.framework.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Logger;

import us.gaaoc.framework.model.Address;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonAddress;



/**
 * @author Sukhada Tapshalkar
 *
 * Nov 19, 2007
 *
 * GAJE_BibbCounty_2.0
 *
 */
public class TextParserFromFile {
	
	//private String[] fields;
	
	private static Logger LOG = Logger.getLogger(TextParserFromFile.class);
	
	/**
	 * @return the fields
	 */
	/*
	public String[] getFields() {
		return fields;
	}
*/
	/**
	 * @param fields the fields to set
	 */
	/*
	public void setFields(String[] fields) {
		fields = fields;
	}
*/
	
	/**
	 * Method to create an array of data fields from a comma separated value
	 * file.
	 * @param fields the fields to set
	 */
	/*
	public String[] textFileToStringArray(InputStream inStream) throws IOException{
		InputStreamReader isr = new InputStreamReader(inStream);
		BufferedReader br = new BufferedReader(isr);
		String lineOfTextFile = new String();		
		String firstLine = new String();		
		firstLine = br.readLine();
		lineOfTextFile = br.readLine();
		this.fields  = lineOfTextFile.split("\",\"");
		return this.fields;	
	}
	*/
	
	/**
	 * This method extracts the court date from the textfile
	 * @return Date
	 * @throws IOException
	 * @throws ParseException
	 */
	/*
	public Date extractCourtDate() throws IOException, ParseException{
		if(fields.length>=202){
			if(fields[202].equalsIgnoreCase("")){
				return null;
			}else{
				Date date = DateUtil.getCurrentDate();
				try {
					date = new SimpleDateFormat("MM/dd/yyyy").parse(fields[202]);  
				} catch (ParseException pe) {
					LOG.info("Error parsing Court Date . . .");
					pe.printStackTrace();
					date = null;
				}
				return date;
			}
		}
		return null;
	}
	*/
	
	/**
	 * This method extracts the externalCaseRexordNumber from the given Text file.
	 * @return String
	 * @throws IOException
	 * @throws ParseException
	 */
	/*
	public String extractExternalCaseRecordNumber() throws IOException, ParseException{
		System.out.println("about to validate extenal");
		String field = fields[51];
		if(field.equalsIgnoreCase("")){
			return null;
		}else{
			return field;
		}
    }
    */
	
	/**
	 * This method returns the CustodianSSN from the given Text File
	 * @return String 
	 * @throws IOException
	 * @throws ParseException
	 */
	/*
	public String extractCustodianSSN() throws IOException, ParseException{
		String field = fields[9];
		if(field.equalsIgnoreCase("")){
			return null;
		}else{
			return field;
		}
    }
    */
	
	/**
	 * This method returns the NonCustodianSSN from the given Text File
	 * @return String
	 * @throws IOException
	 * @throws ParseException
	 */
	/*
	public String extractNonCustodianSSN() throws IOException, ParseException{
		String field = fields[22];
		if(field.equalsIgnoreCase("")){
			return null;
		}else{
			return field;
		}
    }
    */
	
	/**
	 * This method sets the firstName, lastName, middleName, suffixName and date of birth
	 * to the person object
	 * @param fName
	 * @param mName
	 * @param lName
	 * @param sName
	 * @param dob
	 * @return Person
	 */
	public Person populatePersonInfoUsingMultiFieldNameFormat(String fName, 
			String mName, String lName, String sName, String dob) {
		Person genericPerson = new Person();
		genericPerson.setFirstName(fName);
		genericPerson.setMiddleName(mName);
		genericPerson.setLastName(lName);
		genericPerson.setSuffixName(sName);
		genericPerson.setDob(parseDateStringInMMDDYYFormat(dob));
		return genericPerson;
	}
	
	/**
	 * This method sets the fullName and date of birth to the person
	 * @param fullName
	 * @param dob
	 * @return Person
	 */
	public Person populatePersonInfoUsingSingleFieldNameFormat(String fullName, 
			String dob) {
		Person genericPerson = new Person();
		genericPerson.setFirstName(TextParserFromFile.extractFirstName(fullName));
		genericPerson.setMiddleName(extractMiddleName(fullName));
		genericPerson.setLastName(extractLastName(fullName));
		genericPerson.setSuffixName(extractSuffixName(fullName));
		genericPerson.setDob(parseDateStringInMMDDYYFormat(dob));
		return genericPerson;
	}
	
	/**
	 * This method sets the full address, extended address,city, state and zip
	 * to the person address
	 * @param fullAdd
	 * @param extenAdd
	 * @param city
	 * @param state
	 * @param zip
	 * @return PersonAddress
	 *//*
	public static PersonAddress populatePersonInfoAddress(String fullAdd, String extenAdd, String city, String state, String zip) {
		PersonAddress cusPersonAddress = new PersonAddress();
		
		cusPersonAddress.setStreetFullText(fullAdd);
		cusPersonAddress.setStreetExtensionText(extenAdd);
		cusPersonAddress.setCityName(city);
		cusPersonAddress.setState(state);
		cusPersonAddress.setZipCode(zip);
		return cusPersonAddress;
	}*/
	
	/**
	 * This method sets the full address, extended address,city, state and zip
	 * to the address
	 * @param fullAdd
	 * @param extenAdd
	 * @param city
	 * @param state
	 * @param zip
	 * @return PersonAddress
	 */
	public Address populatePersonInfoAddress(String fullAdd, String extenAdd, String city, String state, String zip) {
		Address cusPersonAddress = new Address();
		
		cusPersonAddress.setStreetFullText(fullAdd);
		cusPersonAddress.setStreetExtensionText(extenAdd);
		cusPersonAddress.setCityName(city);
		cusPersonAddress.setState(state);
		cusPersonAddress.setZipCode(zip);
		return cusPersonAddress;
	}

	/**
	 * This method parses the date of birth from the String to the java.util.Date format 
	 * @param dob
	 * @return Date
	 */
	private static Date parseDateStringInMMDDYYFormat(String dob) {	
		Date date = null;
		if(dob!=null){			
			try {
				DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
				// Date date = (Date)formatter.parse("01/29/02");
				date = (Date)formatter.parse(dob);
			} catch(ParseException e) {
				System.out.println("Unable to parse " + dob);
			}
		}
		return date;
	} 
	
	/**
	 * This method sets the street full text,city name, state and zipcode to the personAddress 
	 * @param streetFullText
	 * @param cityName
	 * @param state
	 * @param zipCode
	 * @return PersonAddress
	 */


	/**
	 * 
	 * @param line
	 * @return
	 */
	public static String stripDoubleQuotes(String line){
		String outputString = null;
		if (line != null) {
				if(line.startsWith("\"")){
					outputString = line.substring(1);
					LOG.info("The first strip results in ..."+outputString);
				}else{
					outputString = line;
				}
				if(outputString.endsWith("\"")){
					outputString = outputString.substring(0, outputString.length()-1);
					LOG.info("The second strip results in ..."+outputString);
				}
				LOG.info("The final strip is ..."+outputString);
		} else {
			outputString = line;
		}
		return outputString;
		
	}
	
	/**
	 * This method extracts the first Name by removing the double coats around it.
	 * @param fullName
	 * @return String
	 */
	public static String extractFirstName(String fullName){
		LOG.info("Inside the firstName method..");
		String firstName = "";
		if(fullName.startsWith("\"")){
			fullName = fullName.substring(1);
			LOG.info("The full name after the removal of the first double quotes is .."+fullName);
		}
		String first = " ";
		firstName = fullName.substring(0, fullName.indexOf(first));
		LOG.info("The first name is .."+firstName);
		return firstName.trim();
	}
	
	/**
	 * This method extracts the middle name
	 * @param fullName
	 * @return String
	 */
	public static String extractMiddleName(String fullName){
		LOG.info("Inside the middleName method..");
		String firstName = extractFirstName(fullName);
		String middleName = fullName.substring(firstName.length()+1);
		LOG.info("The full name after the removal of the first name is .."+middleName);
		//middleName = removeAllPrefixedSpaces(middleName);		
		LOG.info("The remaining name after the removal of pr spaces of is.."+middleName);
		String first = " ";
		if(middleName.indexOf(first)>=1){
			middleName = middleName.substring(0, middleName.indexOf(first) );
		}else{
			middleName = "";
		}
		LOG.info("The middle name is .."+middleName);
		return middleName.trim();
	}
	
	/**
	 * This method extracts the last Name
	 * @param fullName
	 * @return {@link String}
	 */
	public static String extractLastName(String fullName){
		LOG.info("Inside the lastName method..");	
		String firstName = extractFirstName(fullName);	
		String middleName = extractMiddleName(fullName);
		String lastName = fullName.substring(firstName.length()+1);
		lastName = lastName.substring(middleName.length()+1);
		//lastName = removeAllPrefixedSpaces(lastName);
		LOG.info("The full name after the removal of the first and middle name is .."+lastName);
		String first = " ";
		if(lastName.indexOf(first)==-1){
			return lastName.trim();
		}else{
			lastName = lastName.substring(0, lastName.indexOf(first));
			return lastName.trim();
		}
	}
	
	/**
	 * This method extracts suffix name
	 * @param fullName
	 * @return {@link String}
	 */
	public static String extractSuffixName(String fullName){
		LOG.info("Inside the Suffixname method..");	
		String firstName = extractFirstName(fullName);	
		String middleName = extractMiddleName(fullName);
		String lastName = extractLastName(fullName);
		String suffixName = fullName.substring(firstName.length()+1);
		suffixName = suffixName.substring(middleName.length()+1);
		LOG.info("The suffix is at this point the last name ..."+suffixName);
		if(suffixName.indexOf(" ")!=-1){
			suffixName = suffixName.substring(lastName.length()+1);
		}else{
			LOG.info("There is no suffix");
			suffixName = "";
		}	
		return suffixName.trim();
	}
	
	/**
	 * @param line
	 * @return
	 */
	static String removeAllPrefixedSpaces(String line){
		String outputString = line;
		String prefixSpace = " ";
		String prefix = outputString.substring(0, 1);
		while(prefix.equals(prefixSpace)){
			outputString = outputString.substring(1);
			prefix = outputString.substring(0, 1);
			
		}
		return outputString;
	}

}
